<?php
namespace app\index\controller;

use think\Controller;

class Index extends Controller
{
    public function index(){
        if(isMobile()){
            $this->redirect('/mobile/index/index');
        }
        $m_id = session('member_id');
        if(!empty($m_id)){
            $member = db('member')->cache(true,60)->where('id',$m_id)->find();
            $this->assign([
                'member' => $member
            ]);
        }
        //轮播图
        $banner = $this->banner();
        //精品推荐
        $recommendation = $this->recommendation();
        //追更榜
        $moreList = $this->moreList();
        //原创精品
        $original = $this->original();
        //新书榜
        $newBook = $this->newBook();
        //原创新书
        $originalNew = $this->originalNew();
        //银票榜
        $ticketList = $this->ticketList();
        //新书入库
        $news = $this->news();
        //最近更新
        $recently = $this->recently();

        $footer = $this->footer();
//                dump($footer);die;
        $this->assign([
            'current' => 'index',
            'banner' => $banner,
            'recommendation' => $recommendation,
            'moreList' => $moreList,
            'original' => $original,
            'newBook' => $newBook,
            'originalNew' => $originalNew,
            'ticketList' => $ticketList,
            'news' => $news,
            'recently' => $recently,
            'footer' => $footer
        ]);
        return view("index");
    }
    //轮播
    public function banner(){
        //state == 1 的时候 轮播图为首页
        $banner = db("banner")
            ->where('state',1)
            ->cache(true,60)
            ->order('id', 'desc')
            ->limit(3)
            ->select();
        return $banner;
    }
    //精品推荐
    public function recommendation(){
        $book = db("book")
            ->cache(true,60)
            ->where('recommendation',1)
            ->order('update','desc')
            ->limit(11)->select();
        return $book;
    }
    // 追更
    public function moreList(){
        $book = db("book")
            ->where('more_list',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(8)->select();
        return $book;
    }
    // 原创精品
    public function original(){
        $original = db("book")
            ->where('original',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(18)->select();
        return $original;
    }

//    新书榜
    public function newBook(){
        $original = db("book")
            ->where('new_book_list',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(10)->select();
        return $original;
    }
//    原创新书
    public function originalNew(){
        $originalNew = db("book")->where('original_new',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(15)->select();
        return $originalNew;
    }
    //银票榜
    public function ticketList(){
        $ticketList = db("book")->where('ticket_list',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(10)->select();
        return $ticketList;
    }
//    新书入库
    public function news(){
        $news = db("book")->where('new',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(19)->select();
        return $news;
    }
    //最近更新
    public function recently(){
        $recently = db("book")->where('recently',1)
            ->order('update','desc')
            ->cache(true,60)
            ->limit(10)->select();
        return $recently;
    }
    public function footer(){
        $data = db("web") ->cache(true,600)->find();
        return $data;
    }
    public function crontab(){
//        获取当前时间戳
        $strto_time = time()+10;
//      查询数据库中 小于当前时间戳的数据
        $min_time_data = db("crontab_chapter")->where('time','<=',$strto_time)->select();
        if(empty($min_time_data))die;
        foreach($min_time_data as $k=>$v){
            //查询这本书章节索引最大值
            $maxChapterIndex = db("chapter")->where('book_id',$v['book_id'])->max('index');
            $book = db("book")->where("id",$v['book_id'])->find();
            $data = [
                'book_id' => $v['book_id'],
                'name' => $v['name'],
                'content_id' => $v['content_id'],
                'is_free' => $book['auto_free'],
                'size' => $v['size'],
                'index' => $maxChapterIndex+1
            ];
            $res = db("chapter")->insert($data);
            if($res){
                db("crontab_chapter")->where('id',$v['id'])->delete();
            }
        }

    }
}
